package com.sgb.goods.viewmodel.inquiry;

import android.text.TextUtils;
import android.util.Log;

import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.sgb.goods.api.GoodsCallBack;
import com.sgb.goods.api.GoodsNet;
import com.sgb.goods.api.UploadNet;
import com.sgb.goods.entity.GoodsInfoTypeEntity;
import com.sgb.goods.entity.inquiry.GoodsDeliverTypeEntity;
import com.sgb.goods.entity.inquiry.GoodsInquiryTypeEntity;
import com.sgb.goods.entity.inquiry.GoodsLoadTypeEntity;
import com.sgb.goods.entity.inquiry.GoodsPayTypeEntity;
import com.sgb.goods.entity.inquiry.GoodsUnloadTypeEntity;
import com.sgb.goods.entity.inquiry.GoodsUploadFileResponseEntity;
import com.sgb.goods.entity.inquiry.InquiryDetailsEntity;
import com.sgb.goods.entity.inquiry.MapLongAndLatEntity;
import com.sgb.goods.entity.inquiry.PriceCheckEntity;
import com.sgb.goods.entity.inquiry.SupplierEntity;
import com.sgb.goods.utils.base.viewmodel.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.LogUtil;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.areapopup.AddressEntity;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import timber.log.Timber;

/**
 * 描述：商品-询价发布
 *
 * @author qiangHeNoo
 * <p>
 * 时间： 2021.12.10
 */
public class InquiryPublishingModel extends BaseViewModel {
    public ObservableField<PriceCheckEntity> priceCheckEntity = new ObservableField<>(new PriceCheckEntity());
    public MutableLiveData<MapLongAndLatEntity> mapLongAndLatEntity = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> addressEntity = new MutableLiveData<>();
    public MutableLiveData<GoodsDeliverTypeEntity> goodsDeliverTypeEntity = new MutableLiveData<>();
    public MutableLiveData<GoodsLoadTypeEntity> goodsLoadTypeEntity = new MutableLiveData<>();
    public MutableLiveData<GoodsUnloadTypeEntity> goodsUnloadTypeEntity = new MutableLiveData<>();
    public MutableLiveData<GoodsInquiryTypeEntity> goodsInquiryTypeEntity = new MutableLiveData<>();
    public MutableLiveData<GoodsPayTypeEntity> goodsPayTypeEntity = new MutableLiveData<>();
    public MutableLiveData<List<SupplierEntity>> vipSupplierEntity = new MutableLiveData<>();
    public MutableLiveData<InquiryDetailsEntity> inquiryDetailsEntity = new MutableLiveData<>();
    public MutableLiveData<GoodsUploadFileResponseEntity> goodsUploadFileResponseEntity = new MutableLiveData<>();
    public MutableLiveData<Boolean> isButtonEnable = new MutableLiveData<>();
    public MutableLiveData<Boolean> isPublishFinished = new MutableLiveData<>();
    private static final String TAG = "PriceCheckModel";

    /**
     * 获取被选择的地址的经纬度信息
     *
     * @param provinceNo
     * @param cityNo
     * @param districtNo
     */
    public void getSelectedAddressLadling(String provinceNo, String cityNo, String districtNo) {
        String areaCode = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.AREA_CODE, "");
        if (TextUtils.isEmpty(districtNo)) {
            GoodsNet.getInstance().getInquiryApi().getLocationLL(areaCode, provinceNo, cityNo).enqueue(new GoodsCallBack<BaseEntity<MapLongAndLatEntity>>() {
                @Override
                public void onSuccess(BaseEntity<MapLongAndLatEntity> data) {
                    if (data != null && data.getData() != null) {
                        mapLongAndLatEntity.postValue(data.getData());
                    }
                }
            });
        } else {
            GoodsNet.getInstance().getInquiryApi().getLocationLL(areaCode, provinceNo, cityNo, districtNo).enqueue(new GoodsCallBack<BaseEntity<MapLongAndLatEntity>>() {
                @Override
                public void onSuccess(BaseEntity<MapLongAndLatEntity> data) {
                    if (data != null && data.getData() != null) {
                        mapLongAndLatEntity.postValue(data.getData());
                    }
                }
            });
        }
    }

    /**
     * 发货地  +  目的地  联合使用
     */
    public void getAddressModel() {
        showLoading();
        GoodsNet.getInstance().getGoodsApi().getCityList().enqueue(new GoodsCallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                if (data != null && data.getData() != null) {
                    addressEntity.postValue(data.getData());
                }
            }
        });
    }

    /**
     * 送货方式
     */
    public void getDeliverDemand(boolean isF) {
        GoodsNet.getInstance().getInquiryApi().getDeliverDemand().enqueue(new GoodsCallBack<BaseEntity<List<GoodsInfoTypeEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<GoodsInfoTypeEntity>> data) {
                if (data != null && data.getData() != null) {
                    goodsDeliverTypeEntity.postValue(new GoodsDeliverTypeEntity(data.getData(), isF));
                }
            }
        });
    }

    /**
     * 装载方式
     */
    public void getLoadDemand(boolean isF) {
        GoodsNet.getInstance().getInquiryApi().getLoadDemand().enqueue(new GoodsCallBack<BaseEntity<List<GoodsInfoTypeEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<GoodsInfoTypeEntity>> data) {
                if (data != null && data.getData() != null && data.getData().size() != 0) {
                    goodsLoadTypeEntity.postValue(new GoodsLoadTypeEntity(data.getData(), isF));
                }
            }
        });
    }

    /**
     * 卸车方式
     */
    public void getUnloadDemand(boolean isF) {
        GoodsNet.getInstance().getInquiryApi().getUnLoadDemand().enqueue(new GoodsCallBack<BaseEntity<List<GoodsInfoTypeEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<GoodsInfoTypeEntity>> data) {
                if (data != null && data.getData() != null && data.getData().size() != 0) {
                    goodsUnloadTypeEntity.postValue(new GoodsUnloadTypeEntity(data.getData(), isF));
                }
            }
        });
    }

    /**
     * 询价方式
     */
    public void getPriceCheckDemand(boolean isF) {
        GoodsNet.getInstance().getInquiryApi().getPriceCheckDemand().enqueue(new GoodsCallBack<BaseEntity<List<GoodsInfoTypeEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<GoodsInfoTypeEntity>> data) {
                if (data != null && data.getData() != null && data.getData().size() != 0) {
                    goodsInquiryTypeEntity.postValue(new GoodsInquiryTypeEntity(data.getData(), isF));
                }
            }
        });
    }

    /**
     * 付款方式
     */
    public void getPayType(boolean isF) {
        GoodsNet.getInstance().getInquiryApi().getPayWay().enqueue(new GoodsCallBack<BaseEntity<List<GoodsInfoTypeEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<GoodsInfoTypeEntity>> data) {
                if (data != null && data.getData() != null && data.getData().size() != 0) {
                    goodsPayTypeEntity.postValue(new GoodsPayTypeEntity(data.getData(), isF));
                }
            }
        });
    }

    /**
     * 上传单张图片
     *
     * @param file
     * @param completeNum
     */
    public void uploadSingleImage(File file, int completeNum, boolean isEnd) {
        showLoading();
        Map<String, RequestBody> photos = new HashMap<>(1);
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("img" + "\"; filename=\"" + completeNum + "image.jpg", requestBody);
        UploadNet.getInstance().getFileUploadApi().uploadFileV1(photos).enqueue(new GoodsCallBack<BaseEntity<List<String>>>() {
            @Override
            public void onSuccess(BaseEntity<List<String>> data) {
                if (data != null && data.getData() != null && data.getData().size() != 0) {
                    goodsUploadFileResponseEntity.postValue(new GoodsUploadFileResponseEntity(data.getData().get(0), file, completeNum, isEnd));
                }
            }
        });
    }

    /**
     * 获取会员供应商
     */
    public void getVipSupplier() {
        showLoading();
        GoodsNet.getInstance().getInquiryApi().getSupplierMember().enqueue(new GoodsCallBack<BaseEntity<List<SupplierEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<SupplierEntity>> data) {
                if (data != null && data.getData() != null && data.getData().size() != 0) {
                    vipSupplierEntity.postValue(data.getData());
                }
            }
        });
    }

    /**
     * 发布询价
     *
     * @param priceCheckEntity 询价实体
     */
    public void publishPriceCheck(PriceCheckEntity priceCheckEntity) {
        if (priceCheckEntity == null) {
            LogUtil.d(TAG, "publishPriceCheck priceCheckEntity is null");
            return;
        }
        Timber.e(new Gson().toJson(priceCheckEntity));
        showLoading();
//        GoodsNet.getInstance().getInquiryApi().publishPriceCheck(priceCheckEntity).enqueue(new ICallBack<BaseEntity<Object>>() {
//            @Override
//            public void onSuccess(BaseEntity<Object> data) {
//                isPublishFinished.postValue(true);
//            }
//        });
        GoodsNet.getInstance().getInquiryApi().publishPriceCheck(priceCheckEntity).enqueue(new GoodsCallBack<BaseEntity<Object>>() {
            @Override
            public void onSuccess(BaseEntity<Object> data) {
                isPublishFinished.postValue(true);
            }

            @Override
            public void onFail() {
                isButtonEnable.postValue(true);
            }
        });
    }

    /**
     * 获取询价详情
     *
     * @param no 询价号
     */
    public void getInquiryDetails(String no) {
        if (priceCheckEntity == null) {
            LogUtil.d(TAG, "publishPriceCheck priceCheckEntity is null");
            return;
        }
        Log.e("tag发布", new Gson().toJson(priceCheckEntity));
        showLoading();
        GoodsNet.getInstance().getInquiryApi().enquiryDetails(no).enqueue(new GoodsCallBack<BaseEntity<InquiryDetailsEntity>>() {
            @Override
            public void onSuccess(BaseEntity<InquiryDetailsEntity> data) {
                if (data != null && data.getData() != null) {
                    inquiryDetailsEntity.postValue(data.getData());
                }
            }

            @Override
            public void onFail() {
                isButtonEnable.postValue(true);
            }
        });
    }

}
